﻿/// <reference path="BasePopupControl.js" />
/// <reference path="../General/SizePos.js" />
/// <reference path="../General/Extensions/Math.js" />
/// <reference path="../General/CSS.js" />

var ____a;


var $ModalPopupPanel = TA.Turbo.Controls.ModalPopupPanel = function ModalPopupPanel(clientId)
{
	this.BasePopupControl(clientId);
}
if (____a)
{
	$ModalPopupPanel.prototype = new TA.Turbo.Controls.BasePopupControl();
	$ModalPopupPanel.prototype.BasePopupControl = new TA.Turbo.Controls.BasePopupControl();
}

TA.Turbo.Controls.ModalPopupPanel.Inherit(TA.Turbo.Controls.BasePopupControl);

TA.Turbo.Controls.ModalPopupPanel.BlockingMode =
{
	None: 0,
	BGOnly: 1,
	BGAndPopups: 2,
	All: 3
};
var $BlockingMode = TA.Turbo.Controls.ModalPopupPanel.BlockingMode;


$ModalPopupPanel._BGBlockerOpacity = 50;

$PropertyDefinition.Define($ModalPopupPanel, "Blocking", "B", $BlockingMode.All);
$PropertyDefinition.Define($ModalPopupPanel, "DefaultButtonId", "DB", "");
$PropertyDefinition.Define($ModalPopupPanel, "OpenArgument", "OA", "");
$PropertyDefinition.Define($ModalPopupPanel, "Title", "Ti", "",
{
	OnChanged: function OnChanged()
	{
		this._spanTitle.innerHTML = this._properties.Title;
	}
});

$ModalPopupPanel.prototype._divBGBlocker = document.getElementById("");
$ModalPopupPanel.prototype._isBGBlocked = false;

$ModalPopupPanel.prototype._allFields = document.getElementById("");
$ModalPopupPanel.prototype._curFocused = document.getElementById("");
$ModalPopupPanel.prototype._defaultButton = document.getElementById("");

$ModalPopupPanel.prototype._divTitle = document.getElementById("");
$ModalPopupPanel.prototype._spanTitle = document.getElementById("");
$ModalPopupPanel.prototype._divHeader = document.getElementById("");
$ModalPopupPanel.prototype._divBody = document.getElementById("");
$ModalPopupPanel.prototype._divFooter = document.getElementById("");

$ModalPopupPanel.prototype._dragStartObjectLocation = new Point(0, 0);
$ModalPopupPanel.prototype._dragStartMouseLocation = new Point(0, 0);
$ModalPopupPanel.prototype._dragging = false;

$ModalPopupPanel.prototype.get_IsBGBlocked = function get_IsBGBlocked()
{
	return this._isBGBlocked;
}
$ModalPopupPanel.prototype.set_IsBGBlocked = function set_IsBGBlocked(value)
{
	this._isBGBlocked = value;
	this._divBGBlocker.style["display"] = this._isBGBlocked ? "block" : "none";
}

$ModalPopupPanel.prototype.Init = function Init()
{
	this.BasePopupControl.Init.call(this);


	this._divTitle = this.get_Control().Find(".title")[0];
	this._spanTitle = this._divTitle.Find("SPAN")[0];
	this._divHeader = this.get_Control().Find(".header")[0];
	this._divBody = this.get_Control().Find(".body")[0];
	this._divFooter = this.get_Control().Find(".footer")[0];

	this._Init_AllFields();

	this._Init_BGBlocker();
}
$ModalPopupPanel.prototype.Load = function Load()
{
	this.BasePopupControl.Load.call(this);

	this._defaultButton = String.IsNullOrEmpty(this.get_DefaultButtonId()) ? null : document.getElementById(this.get_DefaultButtonId());

	if (this.get_Shown())
		this.get_Control().AddEvent("keydown", this.control_KeyDown, this);
	this._divTitle.AddEvent("mousedown", this.divTitle_MouseDown, this);
}


// INTERNAL

// INIT
$ModalPopupPanel.prototype._Init_AllFields = function _Init_AllFields()
{
	/// Store references to all fields so that we can set focus etc.
	this._allFields = this.get_Control().Find("INPUT|SELECT|TEXTAREA");
	for (var i = 0; i < this._allFields.length; ++i)
	{
		var field = this._allFields[i];
		if (field.type == "hidden")
		{ // don't need hidden fields here
			this._allFields.splice(i, 1);
			--i;
			continue;
		}

		field.AddEvent("focus", this.field_Focus, this);
		if (field.tagName == "TEXTAREA")
			field.AddEvent("keydown", this.textAreaField_KeyDown, this);
	}
}
$ModalPopupPanel.prototype._Init_BGBlocker = function _Init_BGBlocker()
{
	var bgBlockDiv = this._divBGBlocker = document.createElement("div");
	bgBlockDiv.id = this.get_Control().id + "_BGBlocker";
	bgBlockDiv.style["position"] =
		document.compatMode == "BackCompat"
		||
		(
			navigator.appVersion.indexOf("MSIE") != -1
			&&
			parseFloat(navigator.appVersion.split("MSIE")[1]) < 7
		)
		? "absolute" : "fixed";
	bgBlockDiv.style["top"] = "0px";
	bgBlockDiv.style["left"] = "0px";
	bgBlockDiv.style["width"] = "100%";
	bgBlockDiv.style["height"] = "100%";
	bgBlockDiv.style["background"] = "gray";
	bgBlockDiv.style["display"] = "none";
	bgBlockDiv.set_Opacity($ModalPopupPanel._BGBlockerOpacity);
	bgBlockDiv.style["fontSize"] = "20000px";
	bgBlockDiv.style["cursor"] = "default";
	bgBlockDiv.style["overflow"] = "hidden";
	bgBlockDiv.onselectstart = function onselectstart() { return false; };
	bgBlockDiv.innerHTML = "&nbsp;";
	var divMain = this.get_Control();
	window.AddEvent("load", function()
	{
		if (document.compatMode == "BackCompat")
			document.body.appendChild(bgBlockDiv);
		else
			divMain.parentNode.insertBefore(bgBlockDiv, divMain);
	});
}
$ModalPopupPanel.prototype._UpdateZIndex = function _UpdateZIndex(iPos)
{
	this.get_Control().style["zIndex"] = 1000 + iPos * 2;
	switch (this.get_Blocking())
	{
		case $BlockingMode.BGOnly:
		case $BlockingMode.BGAndPopups:
			this._divBGBlocker.style["zIndex"] = 999;
			break;
		case $BlockingMode.All:
			this._divBGBlocker.style["zIndex"] = 1000 + iPos * 2 - 1;
			break;
	}
}


// HANDLERS
$ModalPopupPanel.prototype.field_Focus = function field_Focus(e)
{
	this._curFocused = Events.GetSender(e);
}
$ModalPopupPanel.prototype.control_KeyDown = function control_KeyDown(e)
{
	switch (Events.GetKeyCode(e))
	{
		case 27:
			Events.Stop(e);
			this.set_Shown(false);
			break;
		case 13:
			if (this._defaultButton != null)
			{
				Events.Stop(e);
				Events.Cancel(e);
				
				if (this._defaultButton.click != null)
					this._defaultButton.click();
				else if (this._defaultButton.submit != null)
					this._defaultButton.submit();
				else if (this._defaultButton.tagName == "A")
					eval(this._defaultButton.getAttribute("onclick"));
			}
			break;
	}
}
$ModalPopupPanel.prototype.textAreaField_KeyDown = function textAreaField_KeyDown(e)
{
	if (Events.GetKeyCode(e) == 13)
	{
		if (!e.ctrlKey)
			Events.Stop(e);
	}
}
$ModalPopupPanel.prototype._OnShownChanged = function _OnShownChanged()
{
	this.BasePopupControl._OnShownChanged.call(this);

	if (this.get_Shown())
	{
		this._OnShownChanged_True();
	}
	else
	{
		this._OnShownChanged_False();
	}

	for (var iShown = 0; iShown < $ModalPopupPanel.Shown.length; ++iShown)
	{
		var shown = $ModalPopupPanel.Shown[iShown];

		shown._UpdateZIndex(iShown);
	}
}
$ModalPopupPanel.prototype._OnShownChanged_True = function _OnShownChanged_True()
{
	switch (this.get_Blocking())
	{
		case $BlockingMode.BGOnly:
			var already = false;
			for (var iShown = 0; iShown < $ModalPopupPanel.Shown.length; ++iShown)
			{
				var shown = $ModalPopupPanel.Shown[iShown];

				if (shown.get_IsBGBlocked())
					already = true;
			}
			if (!already)
			{
				this.set_IsBGBlocked(true);
			}
			break;
		case $BlockingMode.All:
			this.set_IsBGBlocked(true);
			break;
	}

	$ModalPopupPanel.Shown.push(this);

	for (var i = 0; i < this._allFields.length; ++i)
	{
		var field = this._allFields[i];
		if (!field.disabled)
		{
			try
			{
				field.focus();
				break;
			}
			catch (ex)
			{
			}
		}
	}

	this.get_Control().AddEvent("keydown", this.control_KeyDown, this);
}
$ModalPopupPanel.prototype._OnShownChanged_False = function _OnShownChanged_False()
{
	var previousPopupIndex = $ModalPopupPanel.Shown.IndexOf(this) - 1;
	if (previousPopupIndex >= 0)
	{
		var previousPopup = $ModalPopupPanel.Shown[previousPopupIndex],
			controlToFocus = previousPopup._curFocused;

		try
		{
			controlToFocus.focus();
		}
		catch (excep) { }
	}
	$ModalPopupPanel.Shown.Remove(this);
	this.set_IsBGBlocked(false);

	this.get_Control().RemoveEvent("keydown", this.control_KeyDown, this);
}



// DRAGGING
$ModalPopupPanel.prototype.divTitle_MouseDown = function divTitle_MouseDown(e)
{
	this._dragStartObjectLocation = new Point(
		!String.IsNullOrEmpty(this.get_Control().style["right"])
			? GetNumValueFromStyle(this.get_Control().style["right"])
			: GetNumValueFromStyle(this.get_Control().style["left"]),
		!String.IsNullOrEmpty(this.get_Control().style["bottom"])
			? GetNumValueFromStyle(this.get_Control().style["bottom"])
			: GetNumValueFromStyle(this.get_Control().style["top"])
	);
	this._dragStartMouseLocation = Events.GetMouseCoordsScreen(e);
	this._dragging = true;

	this._divTitle.RemoveEvent("mousedown", this.divTitle_MouseDown, this);

	document.AddEvent("mousemove", this.document_MouseMove, this);
	document.AddEvent("mouseup", this.document_MouseUp, this);

	return false;
}
$ModalPopupPanel.prototype.document_MouseMove = function document_MouseMove(e)
{
	if (this._dragging)
	{
		var p = Events.GetMouseCoordsScreen(e);
		var windowSize = window.get_Size();
		var delta = new Point(p.x - this._dragStartMouseLocation.x, p.y - this._dragStartMouseLocation.y);

		if (!String.IsNullOrEmpty(this.get_Control().style["left"]))
		{
			var left = this._dragStartObjectLocation.x + delta.x;
			left = Math.Limit(left, 0, windowSize.w - 40);
			this.get_Control().style["left"] = left + "px";
		}
		else
		{
			var right = this._dragStartObjectLocation.x - delta.x;
			right = Math.Limit(right, 40 - this.get_Control().offsetWidth, windowSize.w - 40);
			this.get_Control().style["right"] = right + "px";
		}

		if (!String.IsNullOrEmpty(this.get_Control().style["top"]))
		{
			var top = this._dragStartObjectLocation.y + delta.y;
			top = Math.Limit(top, 0, windowSize.h - 40);
			this.get_Control().style["top"] = top + "px";
		}
		else
		{
			var bottom = this._dragStartObjectLocation.y - delta.y;
			bottom = Math.Limit(bottom, 40 - this.get_Control().offsetHeight, windowSize.h - 40);
			this.get_Control().style["bottom"] = bottom + "px";
		}
	}
}
$ModalPopupPanel.prototype.document_MouseUp = function document_MouseUp(e)
{
	this._dragging = false;
	this._divTitle.AddEvent("mousedown", this.divTitle_MouseDown, this);

	document.RemoveEvent("mousemove", this.document_MouseMove, this);
	document.RemoveEvent("mouseup", this.document_MouseUp, this);
}





// STATIC
$ModalPopupPanel.Shown = new Array();


